package com.liugh.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.liugh.annotation.Pass;
import com.liugh.entity.standard.StandardItem;
import com.liugh.mapper.zhongche.ESSearchMapper;
import com.liugh.mapper.zhongche.StandardItemMapper;
import com.liugh.model.ESModel;
import com.liugh.service.IMailService;
import com.liugh.util.PDFUtil;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Iterator;

//@ApiIgnore
//专门用于测试
@RestController
@RequestMapping("/test")
public class TestController {

    private static final Log log = LogFactory.get();
    @Autowired
    ESSearchMapper searchMapper;

    @Autowired
    StandardItemMapper itemMapper;

    @Autowired
    IMailService mailService;

    @GetMapping("/add")
    @Pass
    public void test() throws URISyntaxException, IOException {
        String store = "/var/www/data/zhongcheServer/ESdata/";
        ESModel model = new ESModel();
        StandardItem item = itemMapper.selectById(23);
        model.setAuthor(item.getAuthor());
        model.setContent(PDFUtil.pdfToStringUrl(item.getUploadDir(),store));
        model.setTitle(item.getStandName());
        model.setId(item.getId());
        searchMapper.save(model);
        System.out.println("success: " + model.getContent());
    }

    @GetMapping("/query")
    @Pass
    public void query(@RequestParam String s) throws JsonProcessingException {
        QueryStringQueryBuilder builder = new QueryStringQueryBuilder(s);
        Iterable<ESModel> searchResult = searchMapper.search(builder);
        Iterator<ESModel> iterator = searchResult.iterator();
        System.out.println("=================搜索结果================");
        while (iterator.hasNext()) {
            ESModel article = iterator.next();
            //格式化为json输出
            System.out.println(new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(article));
        }
    }



}
